{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "重置索引（reindex）可以更改原 DataFrame 的行标签或列标签，并使更改后的行、列标签与 DataFrame 中的数据逐一匹配。通过重置索引操作，您可以完成对现有数据的重新排序。如果重置的索引标签在原 DataFrame 中不存在，那么该标签对应的元素值将全部填充为 NaN。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            a         b         c         d\n",
      "第一行  0.208130  0.453369  0.678223  0.606934\n",
      "第二行  0.251465  0.449951  0.374756  0.829590\n",
      "第三行  0.319580  0.846191  0.688477  0.460693\n",
      "第四行  0.214355  0.652832  0.861328  0.869141\n",
      "第五行  0.298828  0.415771  0.900391  0.501465\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "   \"a\": pd.Series(np.random.rand(5),index=[\"第一行\",\"第二行\",\"第三行\",\"第四行\",\"第五行\"]),\n",
    "    \"b\": pd.Series(np.random.rand(5),index=[\"第一行\",\"第二行\",\"第三行\",\"第四行\",\"第五行\"]),\n",
    "     \"c\": pd.Series(np.random.rand(5),index=[\"第一行\",\"第二行\",\"第三行\",\"第四行\",\"第五行\"]),\n",
    "    \"d\": pd.Series(np.random.rand(5),index=[\"第一行\",\"第二行\",\"第三行\",\"第四行\",\"第五行\"])\n",
    "},dtype=np.float16)\n",
    "\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1         2         3         4\n",
      "一  0.501465  0.501465  0.501465  0.501465\n",
      "二  0.501465  0.501465  0.501465  0.501465\n",
      "三  0.501465  0.501465  0.501465  0.501465\n",
      "四  0.501465  0.501465  0.501465  0.501465\n",
      "五  0.501465  0.501465  0.501465  0.501465\n"
     ]
    }
   ],
   "source": [
    "reindexed = df.reindex(index=[\"一\",\"二\",\"三\",\"四\",\"五\"],columns=[1,2,3,4])\n",
    "print(reindexed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            a         b         c         d\n",
      "第一行  0.220459  0.593262  0.894043  0.541992\n",
      "第二行  0.019379  0.768555  0.311523  0.272217\n",
      "第三行  0.655762  0.516113  0.680176  0.151001\n",
      "第四行  0.792969  0.444092  0.328125  0.334473\n",
      "第五行  0.707031  0.302734  0.500977  0.194336\n",
      "------------------------------------------------------------\n",
      "            a         b         c         d\n",
      "第1行  0.967773  0.724609  0.946777  0.442627\n",
      "第2行  0.835449  0.596680  0.895020  0.967773\n",
      "第3行  0.260254  0.604004  0.194092  0.699707\n",
      "第4行  0.408691  0.121155  0.781738  0.858887\n",
      "第5行  0.392334  0.093323  0.429688  0.338135\n",
      "------------------------------------------------------------\n",
      "      a   b   c   d\n",
      "第1行 NaN NaN NaN NaN\n",
      "第2行 NaN NaN NaN NaN\n",
      "第3行 NaN NaN NaN NaN\n",
      "第4行 NaN NaN NaN NaN\n",
      "第5行 NaN NaN NaN NaN\n",
      "------------------------------------------------------------\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "rename() got an unexpected keyword argument \"colums\"",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-37-5bb85fd5ee17>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     20\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf_c\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     21\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"------------------------------------------------------------\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 22\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf_c\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrename\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcolums\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m\"a\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"001\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"b\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"002\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"c\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"003\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"d\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"004\"\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m\"第1行\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"a1\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"第2行\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"a2\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"第3行\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"a3\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"第4行\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"a4\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"第5行\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"a5\"\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mD:\\Progrram Files\\anaconda\\lib\\site-packages\\pandas\\util\\_decorators.py\u001b[0m in \u001b[0;36mwrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    185\u001b[0m         \u001b[1;33m@\u001b[0m\u001b[0mwraps\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    186\u001b[0m         \u001b[1;32mdef\u001b[0m \u001b[0mwrapper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 187\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    188\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    189\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mPY2\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Progrram Files\\anaconda\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36mrename\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m   3779\u001b[0m         \u001b[0mkwargs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'axis'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3780\u001b[0m         \u001b[0mkwargs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'mapper'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3781\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrename\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   3782\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3783\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mSubstitution\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0m_shared_doc_kwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Progrram Files\\anaconda\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36mrename\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m    939\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    940\u001b[0m             raise TypeError('rename() got an unexpected keyword '\n\u001b[1;32m--> 941\u001b[1;33m                             'argument \"{0}\"'.format(list(kwargs.keys())[0]))\n\u001b[0m\u001b[0;32m    942\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    943\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_count_not_none\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0maxes\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: rename() got an unexpected keyword argument \"colums\""
     ]
    }
   ],
   "source": [
    "df_a = pd.DataFrame({\n",
    "   \"a\": pd.Series(np.random.rand(5),index=[\"第一行\",\"第二行\",\"第三行\",\"第四行\",\"第五行\"]),\n",
    "    \"b\": pd.Series(np.random.rand(5),index=[\"第一行\",\"第二行\",\"第三行\",\"第四行\",\"第五行\"]),\n",
    "     \"c\": pd.Series(np.random.rand(5),index=[\"第一行\",\"第二行\",\"第三行\",\"第四行\",\"第五行\"]),\n",
    "    \"d\": pd.Series(np.random.rand(5),index=[\"第一行\",\"第二行\",\"第三行\",\"第四行\",\"第五行\"])\n",
    "},dtype=np.float16)\n",
    "\n",
    "df_b = pd.DataFrame({\n",
    "   \"a\": pd.Series(np.random.rand(5),index=[\"第1行\",\"第2行\",\"第3行\",\"第4行\",\"第5行\"]),\n",
    "    \"b\": pd.Series(np.random.rand(5),index=[\"第1行\",\"第2行\",\"第3行\",\"第4行\",\"第5行\"]),\n",
    "     \"c\": pd.Series(np.random.rand(5),index=[\"第1行\",\"第2行\",\"第3行\",\"第4行\",\"第5行\"]),\n",
    "    \"d\": pd.Series(np.random.rand(5),index=[\"第1行\",\"第2行\",\"第3行\",\"第4行\",\"第5行\"])\n",
    "},dtype=np.float16)\n",
    "\n",
    "df_c = df_a.reindex_like(df_b)\n",
    "print(df_a)\n",
    "print(\"------------------------------------------------------------\")\n",
    "print(df_b)\n",
    "print(\"------------------------------------------------------------\")\n",
    "print(df_c)\n",
    "print(\"------------------------------------------------------------\")\n",
    "print(df_c.rename(columns={\"a\":\"001\",\"b\":\"002\",\"c\":\"003\",\"d\":\"004\"},index={\"第1行\":\"a1\",\"第2行\":\"a2\",\"第3行\":\"a3\",\"第4行\":\"a4\",\"第5行\":\"a5\"}))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
